/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package action;

import java.io.*;
import java.net.*;

import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import negocio.TicketManager;
import persistencia.pojo.Ticket;

/**
 *
 * @author Victor
 */
public class BuscarTicketAction extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String tipo_bus = request.getParameter("tipo_bus");
        String dato_bus = request.getParameter("dato_bus");
        String pag_actual = request.getParameter("pag_actual");  
        String strColTabla = null;
        if(tipo_bus.equals("T")){
            strColTabla="co_ticket";
        }else if(tipo_bus.equals("C")){
            strColTabla="co_cliente";
        }else if(tipo_bus.equals("E")){
            strColTabla="estado";
        }else{
            mensaje(response, "Error obteniendo información.");
            return;
        }
                
        String filasResul="";
        try {
            
            TicketManager mgr = new  TicketManager();
            List<Ticket> listadoTickets = mgr.getTicketsWhere(strColTabla, dato_bus, pag_actual);
              
            for(int i=0; i<listadoTickets.size(); i++){
                filasResul+= filaResultado(listadoTickets.get(i).getCodigo(), listadoTickets.get(i).getCodigo_cliente(), listadoTickets.get(i).getTipo_ticket(), listadoTickets.get(i).getEstado(), listadoTickets.get(i).getDesc_ticket());
            }
            
            Dibujatabla(response, filasResul);             
        
        }catch (Exception e) {
            e.printStackTrace();
        }
    } 
    
    private void mensaje(HttpServletResponse response, String mensaje) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        try {

            out.println(mensaje);
            out.flush();
        } finally { 
            out.close();
        }
    }
    
    private void Dibujatabla(HttpServletResponse response, String strFilasResul) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        try {
            String strTabla = "<table width=\"800px\" border=\"2\">";
            strTabla+="<tr>";
            strTabla+="<td bgcolor=\"#81BEF7\"align=\"center\">Codigo Ticket</td>";
            strTabla+="<td bgcolor=\"#81BEF7\"align=\"center\">Codigo Cliente</td>";
            strTabla+="<td bgcolor=\"#81BEF7\"align=\"center\">Tipo Ticket</td>";
            strTabla+="<td bgcolor=\"#81BEF7\"align=\"center\">Estado</td>";
            strTabla+="<td bgcolor=\"#81BEF7\"align=\"center\">Descripcion</td>";
            strTabla+="</tr>";
            strTabla+=strFilasResul;
            strTabla+="</table>";

            out.println(strTabla);
            out.flush();
        } finally { 
            out.close();
        }
    }
    private String filaResultado(String co_ticket, String co_cliente, String tipo_ticket, String estado, String desc_ticket){
        String fila= "<tr>";
	fila+="<td>"+co_ticket+"</td>";
	fila+="<td>"+co_cliente+"</td>";
	fila+="<td>"+tipo_ticket+"</td>";
	fila+="<td>"+estado+"</td>";
	fila+="<td>"+desc_ticket+"</td>";
	fila+="</tr>";
        
        return fila;
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
